package com.podium.web.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.podium.comment.annotate.OperateLog;
import com.podium.comment.annotate.PermAuth;
import com.podium.comment.AjaxResult;
import com.podium.comment.constant.OperateLogType;
import com.podium.web.system.entity.SysRole;
import com.podium.web.system.entity.SysRoleMenu;
import com.podium.web.system.service.SysRoleMenuService;
import com.podium.web.system.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 邵文泽
 * @description 角色表
 * @date Tue Apr 29 08:39:29 CST 2025
 */
@RestController
@RequestMapping("/system/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    /**
     * 分页获取角色表信息
     *
     * @param sysRole 查询角色表信息条件
     */
    @PermAuth("system:role:list")
    @GetMapping("page")
    public AjaxResult<IPage<SysRole>> getPage(@RequestParam(defaultValue = "1") int pageNum,
                                              @RequestParam(defaultValue = "10") int pageSize,
                                              SysRole sysRole
    ) {
        Page<SysRole> page = new Page<>(pageNum, pageSize);
        return new AjaxResult<IPage<SysRole>>().success(sysRoleService.page(page, sysRole));
    }

    /**
     * 获取角色信息
     *
     * @param sysRole 查询角色信息条件
     */
    @PermAuth("system:role:list")
    @GetMapping("list")
    public AjaxResult<List<SysRole>> getList(SysRole sysRole) {
        return new AjaxResult<List<SysRole>>().success(sysRoleService.list(sysRole));
    }


    /**
     * 根据ID获取角色表信息
     */
    @GetMapping("{id}")
    public AjaxResult<SysRole> getByInfo(@PathVariable Long id) {
        return new AjaxResult<SysRole>().success(sysRoleService.getById(id));
    }

    /**
     * 添加角色表信息
     *
     * @param sysRole 角色表信息
     */
    @OperateLog(title = "系统管理-角色管理", businessType = OperateLogType.ADD)
    @PermAuth("system:role:add")
    @PostMapping("/save")
    public AjaxResult<Void> save(@RequestBody SysRole sysRole) {
        boolean row = sysRoleService.save(sysRole);
        return row ? new AjaxResult<Void>().success() : new AjaxResult<Void>().error();
    }

    /**
     * 更新角色表信息
     *
     * @param sysRole 角色表信息
     */
    @OperateLog(title = "系统管理-角色管理", businessType = OperateLogType.EDIT)
    @PermAuth("system:role:edit")
    @PostMapping("/update")
    public AjaxResult<Void> update(@RequestBody SysRole sysRole) {
        boolean row = sysRoleService.updateById(sysRole);
        return row ? new AjaxResult<Void>().success() : new AjaxResult<Void>().error();
    }

    /**
     * 删除角色表信息
     *
     * @param ids 角色表信息ID集合
     */
    @OperateLog(title = "系统管理-角色管理", businessType = OperateLogType.DELETE)
    @PermAuth("system:role:delete")
    @GetMapping("/remove/{ids}")
    public AjaxResult<Void> remove(@PathVariable Long[] ids) {
        if (ids == null || ids.length == 0) {
            return new AjaxResult<Void>().error("没有要删除的数据！");
        }
        for (Long id : ids) {
            sysRoleService.removeById(id);
        }
        return new AjaxResult<Void>().success();
    }

    @PermAuth("system:role:allocation")
    @GetMapping("/listRoleMenu/{roleId}")
    public AjaxResult<List<SysRoleMenu>> listRoleMenu(@PathVariable Long roleId) {
        SysRoleMenu sysRoleMenu = new SysRoleMenu();
        sysRoleMenu.setRoleId(roleId);
        return new AjaxResult<List<SysRoleMenu>>().success(sysRoleMenuService.list(sysRoleMenu));
    }

    /**
     * 修改角色菜单关联
     */
    @OperateLog(title = "角色菜单关联", businessType = "分配菜单")
    @PermAuth("system:role:allocation")
    @PostMapping("/editRoleMenu")
    public AjaxResult<Void> editRoleMenu(@RequestBody SysRoleMenu sysRoleMenu) {
        sysRoleMenuService.removeByRoleId(sysRoleMenu.getRoleId());
        boolean row = sysRoleMenuService.saveBatch(sysRoleMenu);
        return row ? new AjaxResult<Void>().success() : new AjaxResult<Void>().error();
    }
}